#pragma warning disable 0219
using UnityEngine;
using System;
using System.Linq;
using System.Collections;
using Hont.C2UTool;

public class C2UStaticInfoUnityCloth : MonoBehaviour, IC2UStaticInfoReceiver
{
    public string content;


    public void OnStaticInfoReplaced(GameObject root)
    {
    }

    public void OnStaticInfoReplacedLate(GameObject root)
    {
        var cloth = gameObject.AddComponent<Cloth>();

        var str1 = content.Split('|');

        var weightsStr = str1[0].Split('*')[1];
        var stretching_StiffnessStr = str1[1].Split('*')[1];
        var bending_StiffnessStr = str1[2].Split('*')[1];
        //var use_TethersStr = str1[3].Split('*')[1];
        var use_GravityStr = str1[4].Split('*')[1];
        var dampingStr = str1[5].Split('*')[1];
        var external_AccelerationStr = str1[6].Split('*')[1];
        var random_AccelerationStr = str1[7].Split('*')[1];
        var world_Velocity_ScaleStr = str1[8].Split('*')[1];
        var world_Acceleration_ScaleStr = str1[9].Split('*')[1];
        var frictionStr = str1[10].Split('*')[1];
        var collision_Mass_ScaleStr = str1[11].Split('*')[1];
        //var use_Continous_CollisionStr = str1[12].Split('*')[1];
        //var useVirtualParticlesStr = str1[13].Split('*')[1];
        //var solverFrequencyStr = str1[14].Split('*')[1];
        var sleepThresholdStr = str1[15].Split('*')[1];
        var collidersStr = str1[16].Split('*')[1];

        var weightsInfoCollection = weightsStr
            .Split(';')
            .Select(m =>
        {
            var temp = m.Split(',');
            var weightInfo = new { Position = new Vector3(float.Parse(temp[0]), float.Parse(temp[1]), float.Parse(temp[2])), Weight = float.Parse(temp[3]) };
            return weightInfo;
        });

        var coefficients = cloth.coefficients;
        var vertices = cloth.vertices;

        for (int i = 0; i < vertices.Length; i++)
        {
            var vertex = vertices[i];
            var vertexPosition = transform.TransformPoint(vertex);

            var weightInfo = weightsInfoCollection.FirstOrDefault(m => vertexPosition == m.Position);

            if (weightInfo == null) continue;

            coefficients[i].maxDistance = weightInfo.Weight;
        };
        cloth.coefficients = coefficients;

        cloth.stretchingStiffness = float.Parse(stretching_StiffnessStr);
        cloth.bendingStiffness = float.Parse(bending_StiffnessStr);
        //use_TethersStr  no interface?
        cloth.useGravity = use_GravityStr == "True" ? true : false;
        cloth.damping = float.Parse(dampingStr);
        var tempStrArray = external_AccelerationStr.Split(',');
        cloth.externalAcceleration = new Vector3(
            float.Parse(tempStrArray[0]),
            float.Parse(tempStrArray[1]),
            float.Parse(tempStrArray[2]));

        tempStrArray = random_AccelerationStr.Split(',');
        cloth.randomAcceleration = new Vector3(
            float.Parse(tempStrArray[0]),
            float.Parse(tempStrArray[1]),
            float.Parse(tempStrArray[2]));

        cloth.worldVelocityScale = float.Parse(world_Velocity_ScaleStr);
        cloth.worldAccelerationScale = float.Parse(world_Acceleration_ScaleStr);
        cloth.friction = float.Parse(frictionStr);
        cloth.collisionMassScale = float.Parse(collision_Mass_ScaleStr);
        //cloth.useContinuousCollision = use_Continous_CollisionStr; Holy shit...
        //cloth.useVirtualParticles = use_GravityStr == "True" ? true : false; Holy shit...
        //cloth.solverFrequency = float.Parse(solverFrequencyStr)  Holy shit...
        cloth.sleepThreshold = float.Parse(sleepThresholdStr);

#if UNITY_EDITOR
        if (!string.IsNullOrEmpty(collidersStr))
        {
            var capsuleCollidersCollection = collidersStr
                .Split(';')
                .Select(m =>
                {
                    var path = C2UUtility.SecurityStringDecode(m);
                    var collider = root.transform.Find(path).GetComponent<CapsuleCollider>();
                    return collider;
                });
            cloth.capsuleColliders = capsuleCollidersCollection.ToArray();
        }
#endif

        DestroyImmediate(this);
    }
}
